package middleware

import (
	"app/clientutils/commonfunc"
	"app/commonutils/jwtauth"
	"github.com/gin-gonic/gin"
)

func Authorization() gin.HandlerFunc {
	return func(c *gin.Context) {
		tokenString := c.Request.Header.Get("token")
		if tokenString == "" {
			c.Abort()
			commonfunc.ReqUnauthorized(c,"Unauthorized")
			return
		}
		tokenString, err := jwtauth.RefreshToken(jwtauth.Key,tokenString)
		if err != nil {
			c.Abort()
			commonfunc.ReqUnauthorized(c,"Unauthorized")
			return
		}
		c.Header("token",tokenString)
		c.Next()
	}
}
